/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package provider.impl.genomemap.data;

import commons.util.ArrayUtil;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Susanta Tewari
 * @version 1.0 Dec 28, 2010
 */
abstract class GenotypeMissingValAlgo {

    /** Field description */
    public static final int MISSING_VALUE_CODE = 9;

    /**
     * @param data genotype data on which the algorithm is to be applied
     * rows indicate samples and columns genes
     * @return new genotype data after applying the algorithm
     */
    abstract byte[][] apply(final byte[][] data);

    static GenotypeMissingValAlgo getNearestScoreAlgo() {

        return new GenotypeMissingValAlgo() {

            @Override
            byte[][] apply(final byte[][] data) {

                final byte[][] result = ArrayUtil.clone(data);


                // number of missing values in the first column (adhoc imputation)
                int firstColumnCounter = 0;


                // logical imputation (score of the nearest. Genotype data has markers in their known order)
                int otherColCounter = 0;

                for (int i = 0; i < result.length; i++) {

                    if (result[i][0] == 9) {

                        result[i][0] = 1;

                        firstColumnCounter++;
                    }

                    for (int j = 1; j < result[0].length; j++) {

                        if (result[i][j] == 9) {

                            result[i][j] = result[i][j - 1];

                            otherColCounter++;
                        }
                    }
                }

                Logger.getLogger(this.getClass().getName()).log(Level.INFO,
                                 "Total first-column missing values: " + firstColumnCounter + ". "
                                 + "Total other-column missing values: " + otherColCounter);

                return result;
            }
        };
    }
}
